package com.tianqu.tms.www.rental.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.www.rental.actionform.RentalLocationAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;;

public class RentalLocationAddResultController extends TmsController {
	private Logger logger = Logger.getLogger(RentalLocationAddResultController.class);
	private int userId = 0;

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TmsUserProfile file = uc.getUserProfile();
		this.userId = file.getUniqueId();
		TxController controller = TxController.getTxController();
		RentalLocationViewHelper view = (RentalLocationViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);

		// 添加事务提交异常捕获
		try {
			controller.begin();

			RentalLocationAddInputForm form = (RentalLocationAddInputForm) uc.getInputForm();
			view.setLocationSerial(form.getLocationSerial());
			view.setRentalType(form.getRentalType());
			view.setAcreage(form.getAcreage());
			view.setCarPort(form.getCarPort());
			view.setAddress(form.getAddress());
			view.setRentalArea(form.getRentalArea());
			view.setAnnualTransactionVolume(form.getAnnualTransactionVolume());
			
			RentalLocationInfo rentalLocationInfo = RentalLocationInfo.getInstance();
			
			if (!rentalLocationInfo.isExistLocationInfoBySerial(form.getLocationSerial())) {
				//
				rentalLocationInfo.setAnnualTransactionVolume(form.getAnnualTransactionVolume());
				
				// 租位编号
				rentalLocationInfo.setLocationSerial(form.getLocationSerial());
				
				// 租位类型
				rentalLocationInfo.setRentalType(form.getRentalType());
				
				// 租位面积
				rentalLocationInfo.setAcreage(StringUtil.getint(form.getAcreage()));
				
				// 车位数量
				rentalLocationInfo.setCarPort(StringUtil.getint(form.getCarPort()));
				
				// 插入用户ID
				rentalLocationInfo.setInsertUserId(userId);
				
				// 更新用户ID
				rentalLocationInfo.setUpdateUserId(userId);
				
				// 租位位置
				rentalLocationInfo.setAddress(form.getAddress());
				
				// 租位区域
				rentalLocationInfo.setArea(form.getRentalArea());
				
				// 插入 市场租位信息表（tms_rentalLocation_info）
				rentalLocationInfo.insertLocationInfo(rentalLocationInfo);
				
				// 操作类型
				rentalLocationInfo.setHandleType(1);//1：租位登记
				
				// 插入 租位操作记录表（tms_rental_update_histroy）
				rentalLocationInfo.insertRentalUpdateHistroy(rentalLocationInfo);
				
			} else {
				
				// 租位编号已存在，无法进行保存
				uc.setAttribute("locationSerial","false");
			}
			
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}

